<?PHP



// -------------------------------------------------------------------------------------------------------
function GetExt($Filename) {
        $RetVal = explode ( '.', $Filename);
        return $RetVal[count($RetVal)-1];
}

// -------------------------------------------------------------------------------------------------------
function GetMicrotime() {
        list($usec, $sec) = explode(" ",microtime());
        return ((float)$usec + (float)$sec);
}

// -------------------------------------------------------------------------------------------------------
function UploadLockReadable ($CurrentFile) {
        chmod ($CurrentFile, 0644);
}

// -------------------------------------------------------------------------------------------------------
function UploadCreateThumb ($src_file, $dest_file, $newWidth) {
        // find the image size & type
        if(!function_exists('imagecreate')){return $src_file;}

        $imginfo = @getimagesize($src_file);
        switch($imginfo[2]) {
                case 1: $type = IMG_GIF; break;
                case 2: $type = IMG_JPG; break;
                case 3: $type = IMG_PNG; break;
                case 4: $type = IMG_WBMP; break;
                default: return $src_file; break;
        }
        #echo $imginfo[2].IMG_GIF.IMG_JPG.$type.$src_file;
        switch($type) {
                case IMG_GIF:
                        if(!function_exists('imagecreatefromgif')){return $src_file;}
                         $srcImage = @imagecreatefromgif($src_file);
                         echo  "gif->".$srcImage."<br>".$src_file;
                        break;
                case IMG_JPG:
                        if(!function_exists('imagecreatefromjpeg')){return $src_file;}
                        $srcImage = @imagecreatefromjpeg($src_file);
                        break;
                case IMG_PNG:
                        if(!function_exists('imagecreatefrompng')){return $src_file;}
                        $srcImage = @imagecreatefrompng($src_file);
                        break;
                case IMG_WBMP:
                        if(!function_exists('imagecreatefromwbmp')){return $src_file;}
                        $srcImage = @imagecreatefromwbmp($src_file);
                        break;
                default: return $src_file;
        }

        if($srcImage){
                // height/width
                $srcWidth   = $imginfo[0];
                $srcHeight  = $imginfo[1];
                $ratioWidth = $srcWidth/$newWidth;
                $destWidth  = $newWidth;
                $destHeight = $srcHeight / $ratioWidth;
                // resize
                $destImage  = @imagecreate($destWidth, $destHeight);
                echo imagecopyresized($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
                // create and save final picture
                echo "image dest".$destImage."<br>";
                switch($type){
                        case IMG_GIF:  @imagegif ($destImage, $dest_file); break;
                        case IMG_JPG:  @imagejpeg($destImage, $dest_file); break;
                        case IMG_PNG:  @imagepng ($destImage, $dest_file); break;
                        case IMG_WBMP: @imagewbmp($destImage, $dest_file); break;
                }

                // free the memory
                @imagedestroy($srcImage);
                @imagedestroy($destImage);

                return $dest_file;
        } else {
                return $src_file;
        }
}
####################################
function ShowUploadDialog () {
####################################
        global $admin,$module_name, $cookie, $UploadAllowedExt, $MAXFILESIZE, $UploadPath;

        $uname    = cookiedecode($user);
        $username = $uname[1];

        $random   = GetMicrotime();


        echo "<html style=\"width: 420px; height: 216px;\">";
        echo "<head>";
        echo "<title>"._UPLOADNEWIMAGE."</title>";
        echo "<style> html, body, button, div, input, select, fieldset { font-family: MS Shell Dlg; font-size: 8pt; position: absolute; }; </style>";
        echo "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>";

        AddCancelScript();

        echo "<script>";
        echo "function insertImage() {\n";
        echo "        var img = window.event.srcElement;\n";
        echo "        if (img) {\n";
        echo "                var imagesrc       = document.all['filename'].value;\n";
        echo "                var extention      = imagesrc.substr(imagesrc.lastIndexOf(\".\"));\n";
        echo "                var alttext        = document.all['altfilename'].value;\n";
        echo "                window.returnValue = '<a href=\"".$UploadPath.$username.$random."'+ extention +'\" target=\"_blank\"><img src=\"".$UploadPath."tn".$username.$random."'+ extention +'\" alt=\"'+ alttext +'\" border=0 align=absmiddle></a>';\n";
        echo "                window.close();\n";
        echo "        }\n";
        echo "}\n";
        echo "</script>";

        echo "</head>";
        echo "<body style='background: threedface; color: windowtext;'>";
        if (is_admin($admin)) {
                echo "<form name=mydialog enctype='multipart/form-data' method=POST action='modules.php?name=$module_name&op=upload&rnd=$random'>";
                echo "<input ID=MAX_FILE_SIZE name=MAX_FILE_SIZE type=hidden value=".$MAXFILESIZE.">";
                echo "<fieldset id=fldLayout style='left: 0.50em; top: 0.5em; width: 38.0em; height: 14.3000em;'></fieldset>";
                echo "<div id=divFileName style='left: 1.50em; top: 1.5em; width: 36.5em; height: 2.5000em;'><center>"._ONLY1IMAGE."</center></div>";
                echo "<div id=divFileName style='left: 1.50em; top: 4.5em; width: 29.0em; height: 1.2168em;'>"._MAXFILESIZE."</div>";
                echo "<div id=divFileName style='left: 11.25em; top: 4.5em; width: 29.0em; height: 1.2168em;'><b>".($MAXFILESIZE / 1024)."&nbspKo</b></div>";
                echo "<div id=divFileName style='left: 1.50em; top: 6.2em; width: 29.0em; height: 1.2168em;'>"._ALLOWEDEXTENTION."</div>";
                echo "<div id=divFileName style='left: 11.25em; top: 6.2em; width: 29.0em; height: 1.2168em;'><b>";
                while (list ( $key, $val) = each ($UploadAllowedExt)) {
                        echo "*.".$val."&nbsp;";
                }
                echo "</b></div>";
                echo "<div id=divFileName style='left: 1.50em; top: 9.8em; width: 29em; height: 1.2168em;'><b>"._FILENAME."</b></div>";
                # le bouton browser
                echo "<input style='left: 11.25em; top: 9.3em; width: 19.75em; height: 2.1294em;' ID=filename value= Desktop name=filename type=file tabIndex=10 onfocus='select()'>";
                echo "<input style='left: 31.25em; top: 9.3em; width: 6em; height: 2.2000em;' value="._BTNUPLOAD." type=submit >";
                echo "<div id=divFileName style='left: 1.50em; top: 12.3em; width: 29em; height: 1.2168em;'><b>"._FILEALTNAME."</b></div>";
                echo "<input style='left: 11.25em; top: 11.8em; width: 19.75em; height: 2.1294em;' id=altfilename name=altfilename tabIndex=30 onfocus='select()'>";
                echo "<input style='left: 13.00em; top: 15.3em; width: 6em; height: 2.2000em;' value="._BTNENTER."  type=button onClick='insertImage();'>";
                echo "<input style='left: 21.00em; top: 15.3em; width: 6em; height: 2.2000em;' value="._BTNCANCEL." type=button onClick='cancel();'>";
                echo "</form>";
        } else {
                echo "<script>";
                echo "alert('"._ONLYREGITERUSERCANUPLOAD."');\n";
                echo "cancel();\n";
                echo "</script>";
        }
        echo "</body></html>";
}

// -------------------------------------------------------------------------------------------------------
function DoFileUpload ($InputFile, $MaxSize, $UploadPath, $ErrorFunction, $ExtsOk, $ForceFilename, $OverwriteOk) {
        $ErrNo = -1;

        $TempFile = $_FILES[$InputFile]['tmp_name'];
        $FileSize = $_FILES[$InputFile]['size'];
        $FileName = $_FILES[$InputFile]['name'];
        $FileType = $_FILES[$InputFile]['type'];

        if (strlen($ForceFilename)) {
                $FileName = $ForceFilename.$random;
        }
        ###########
        if (!function_exists($ErrorFunction)) {
                if (!function_exists('DoFileUploadDefErrorHandle')) {
                        function DoFileUploadDefErrorHandle($ErrorNumber, $ErrorText) {

                                echo "<script>alert('".$ErrorText."');</script>\n";
                                echo "<script>window.close();</script>\n";
                                echo "</Body>\n";
                                echo "</html>\n";
                        }
                }
                $ErrorFunction = 'DoFileUploadDefErrorHandle';
        }
        ############

        echo "<html style='width: 50px; height: 30px;'>";
        echo "<head>";
        AddCancelScript();
        echo "</head>";
        echo "<body>\n";
        echo "<center><b>"._PLEASEWAIT."</b></center>";

        flush();

        if($TempFile == 'none' || $TempFile == '') {
                $ErrorTxt = _UNSPECIFIED;
                $ErrNo    = 1;
                $ErrorFunction($ErrNo, $ErrorTxt);
                return $ErrNo;
        }

        if(!is_uploaded_file($TempFile)) {
                $ErrorTxt = _FILEATTACK." \"".$FileName."\"";
                $ErrNo    = 2;
                $ErrorFunction($ErrNo, $ErrorTxt);
                return $ErrNo;
        }

        if($FileSize == 0) {
                $ErrorTxt = _ZEROLENGTHFILE;
                $ErrNo    = 3;
                $ErrorFunction($ErrNo, $ErrorTxt);
                return $ErrNo;
        }

        $TheExt = GetExt($FileName);
        foreach ($ExtsOk as $CurNum => $CurText) {
                if (strtolower($TheExt) == strtolower($CurText)) {
                        $FileExtOk = 1;
                }
        }

        if($FileExtOk != 1) {
                $ErrorTxt = _UPLOADDISALLOWEDEXTENTION;
                $ErrNo    = 4;
                $ErrorFunction($ErrNo, $ErrorTxt);
                return $ErrNo;
        }

        if($FileSize > $MaxSize) {
                $ErrorTxt = _UPLOADEXCEEDEDMAXFILESIZE . ($MaxSize / 1024) . '&nbsp;Ko.';
                $ErrNo    = 5;
                $ErrorFunction($ErrNo, $ErrorTxt);
                return $ErrNo;
        }

        if(file_exists($UploadPath.$FileName) && !strlen($OverwriteOk)) {
                $ErrorTxt = _ALREADYEXISTS;
                $ErrNo    = 6;
                $ErrorFunction($ErrNo, $ErrorTxt);
                return $ErrNo;
        }

        move_uploaded_file ($TempFile, $UploadPath.$FileName);
        $SrcFile = $UploadPath.$FileName;
        $DesFile = $UploadPath."".$FileName;
        #$result  = UploadCreateThumb($SrcFile, $DesFile, 120);
        echo $result."<br>";
        UploadLockReadable ($SrcFile);
        UploadLockReadable ($DesFile);
        #echo "<script>window.close();</script>\n";
        echo "</body>\n";
        echo "</html>\n";
        #echo $ErrNo;
        return $ErrNo;
}

$ForceFilename    = $uname . $random;

?>
